import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel  {
    Scene1 scene1 = new Scene1();
    Plant1 plant1 = new Plant1();
    InitC C = new InitC();
    public GamePanel(){
        KeyAdapter adapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                switch (code){
                    case KeyEvent.VK_A:
                        C.x-=20;
                        break;
                    case KeyEvent.VK_D:
                        C.x+=20;
                        break;
                }
                repaint();

            }
        };
        addKeyListener(adapter);
        setBackground(Color.BLACK);
    }

    int flag = 0;


    public void paint(Graphics g) {
        super.paint(g);
        while (flag == 0) {
            Thread plant = new MyThread(g);
            plant.start();
            flag++;
        }
        g.drawImage(scene1.map.get("scene1-background"), 0, 0, null);
        g.drawImage(scene1.map.get("scene1-C"), C.x, C.y, null);
        g.drawImage(plant1.img, 300, 400, null);
        if (C.x <= 500) {
            g.drawImage(scene1.map.get("scene1-speak"), 320, 300, null);
            g.setColor(Color.BLACK);
            g.drawString("你要参与组成我吗", 360, 365);
            g.drawString("(点击此处确认)", 375, 379);
        }
        g.drawImage(scene1.map.get("scene1-Sheep"), 1500, 400, null);
    }

        class MyThread extends Thread {
            Graphics g;
            int i = 1;

            public MyThread(Graphics g) {
                this.g = g;
            }

            public void run() {

                while (true) {
                    plant1.changeImage();

                    try {
                        repaint();
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        }


    }
